package com.wcsy.myblog.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcsy.myblog.dao.ArticleMapper;
import com.wcsy.myblog.dao.CommentMapper;
import com.wcsy.myblog.dao.TypeMapper;
import com.wcsy.myblog.dao.UserMapper;
import com.wcsy.myblog.entity.*;
import com.wcsy.myblog.filter.SensitiveFilter;
import com.wcsy.myblog.service.IAttentionService;
import com.wcsy.myblog.service.ILkplusService;
import com.wcsy.myblog.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class NumServiceImpl {
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    HttpSession session;
    @Autowired
    ILkplusService  iLkplusService;
    @Autowired
    IUserService iUserService;
    @Autowired
    IAttentionService  iAttentionService;
    @Autowired
    UserMapper userMapper;
    /**
     * 清空未查看的数据
     *
     */
    public void deleteNo(String name){
        User user = (User) session.getAttribute("user");
//           User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", 1));
        if (name.equals("like")){
            //当前用户未查看的点赞数
            int noLike = (int) user.getNoLike();
           user.setUserLike(user.getUserLike()+noLike);
           user.setNoLike(0);
            iUserService.update(user, new QueryWrapper<User>().eq("user_id", user.getUserId()));
        }else if (name.equals("comment")){
            //当前用户未查看的评论数
            int userComment = (int) user.getUserComment();
            user.setUserComment(0);
            iUserService.update(user, new QueryWrapper<User>().eq("user_id", user.getUserId()));
        }else if (name.equals("Fan")){
            iAttentionService.deleteAttontion((int) user.getUserId());
        }else if (name.equals("announcement")){
            int noAnnouncement = (int) user.getNoAnnouncement();
            user.setNoAnnouncement(0);
            iUserService.update(user, new QueryWrapper<User>().eq("user_id", user.getUserId()));
        }
    }

    /**
     * @apiNote 用户的各项数量
     * @return
     */
    public Map<String,Integer> nub(){
        Map<String, Integer> list = new LinkedHashMap<>();
        User user = (User) session.getAttribute("user");
        List<Article> articles = articleMapper.select(user.getUserName());
        //用户的文章数量
        int sizeArticle = articles.size();
        List<Comment> comments = commentMapper.listComment(user.getUserName());
        //用户的评论数量
        int sizeComment = comments.size();
//        List<Type> types = typeMapper.listType(user.getUserName());
        //用户的文章类型数量
        List<Type> types = typeMapper.listTypeId((int) user.getUserId());
        int sizeType = types.size();

        List<Article> articlesCollect = articleMapper.SelectListCollect((int)user.getUserId());
        //用户的收藏文章数量
        int sizeArticlesCollect  = articlesCollect.size();
        list.put("sizeArticle",sizeArticle);
        list.put("sizeComment",sizeComment);
        list.put("sizeType",sizeType);
        list.put("sizeArticlesCollect",sizeArticlesCollect);
        return list;
    }

    /**
     * @param file 用户上传的文件
     * @return 用户的图片
     */
    public  String getfile(MultipartFile file) {
        String page ="";
        if (file != null) {
            //获取上传的文件名
            String filename = file.getOriginalFilename();
            //截取后后缀名
            String suffix = filename.substring(filename.lastIndexOf("."));
            //使用当前时间创建新文件名
            filename = System.currentTimeMillis() + suffix;

            File file2  = new File("src\\main\\resources\\static\\img\\"+filename);
            File file1 = file2.getAbsoluteFile();
//            File file1  = new File("G:\\img\\"+filename);
            System.out.println(file1);
            //上传文件
            try {
                file.transferTo(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            page = "/img/" + filename;
        }
        return page;
    }

    /**
     * @apiNote 敏感词过滤
     * @param sentence  待过滤的句子
     * @return
     */
    public String  getfilter(String sentence) {
        // 使用默认单例（加载默认词典）
        SensitiveFilter filter = SensitiveFilter.DEFAULT;
        //将sentence在词库中查找
        String filted = filter.filter(sentence, '*');
        return filted;
    }

    /**
     * 新增的未查看的点赞 评论 粉丝
     * @return
     */
    public Map<String,Integer> getNumber(){
        Map<String, Integer> map = new LinkedHashMap<>();
        User user = (User) session.getAttribute("user");
//       User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", 1));
        //根据用户id查出未查看的点赞数
        int noLike = (int) user.getNoLike();
        map.put("nolike",noLike);
        //根据用户id查出未查看的评论数
        int userComment = (int) user.getUserComment();
        map.put("userComment",userComment);

        //用户未查看的公告数
        int noAnnouncement = (int) user.getNoAnnouncement();
        map.put("noAnnouncement",noAnnouncement);

        //根据用户id查出未查看的粉丝数
        List<Attention> attentions = iAttentionService.list(new QueryWrapper<Attention>().eq("attention_id", user.getUserId()));
        int userFan= 0;
        for (int i = 0; i < attentions.size(); i++) {
            userFan  +=attentions.get(i).getUserFan();
        }
         map.put("userFan",userFan);
        return map;
    }
}
